﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace cn.Util
{
    public class StringUtils
    {
        #region 判断是否为数字
        /// <summary>
        /// 判断是否为数字
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool isdigit(string s)
        {
            if (s == null || s.Length == 0) return false;

            for (int i = 0; i < s.Length; i++)
                if (s[i] < '0' || s[i] > '9') return false;

            return true;
        }
        #endregion

        #region 判断是否为float类型的数据
        /// <summary>
        /// 判断是否为float类型的数据
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool isfloat(string s)
        {
            if (s == null || s.Length == 0) return false;

            for (int i = 0, j = 0; i < s.Length; i++)
            {
                if (s[0] == '-') continue;
                if (s[i] == '.')
                {
                    j++;
                    if (j > 1) return false;
                    else continue;
                }
                if (s[i] < '0' || s[i] > '9') return false;
            }
            return true;
        }
        #endregion

        #region  判断是否为中文
        /// <summary>
        /// 判断是否为中文
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool isChinese(string str)
        {
            string re = @"^[\u4E00-\u9FA5\uE815-\uFA29]+$";
            return Regex.IsMatch(str, re);
        }
        #endregion

        #region 是否为有效的SBC
        /// <summary>
        /// 是否为有效的SBC
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool isSBC(string str)
        {
            string re = @"^[^\x00-\xff]$";
            return Regex.IsMatch(str, re);
        }
        #endregion

        #region 是否为有效的IP2V
        /// <summary>
        /// 是否为有效的IP2V
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool isIP2V(string str)
        {
            string re = @"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$";
            return Regex.IsMatch(str, re);
        }
        #endregion

        #region 判断是否为整数
        /// <summary>
        /// 判断是否为整数
        /// </summary>
        /// <param name="str"></param>
        /// <param name="isCanHaveStartWithZero">是否允许以0开始</param>
        /// <returns></returns>
        public static bool isInteger(string str, bool isCanHaveStartWithZero)
        {
            string re = @"^\d+$";
            if (Regex.IsMatch(str, re))
            {
                if (isCanHaveStartWithZero)
                {
                    return !(str.Length > 1 && str[0] == '0');
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 判断是否为有效的电话号码
        /// <summary>
        /// 判断是否为有效的电话号码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        //public static bool isTel(string str)
        //{
        //    string re = @"^(?:(?:\d{1,3}-){0,1}(?:\d{3,4}-){1}){0,1}\d{7,8}$";
        //    if (Regex.IsMatch(str, re))
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        re = @"^(?:\(\d{3,4}\)){0,1}\d{7,8}$";
        //        if (Regex.IsMatch(str, re))
        //        {
        //            return true;
        //        }
        //        else
        //        {
        //            // (86)0754-3598124
        //            re = @"^\(\d{1,3}\)\d{3,4}-\d{7,8}$";
        //            return Regex.IsMatch(str, re);
        //        }
        //    }
        //}
        /// <summary>
        /// 匹配3位或4位区号的电话号码，其中区号可以用小括号括起来，
        /// 也可以不用，区号与本地号间可以用连字号或空格间隔，
        /// 也可以没有间隔
        /// \(0\d{2}\)[- ]?\d{8}|0\d{2}[- ]?\d{8}|\(0\d{3}\)[- ]?\d{7}|0\d{3}[- ]?\d{7}
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsPhone(string input)
        {
            //string pattern = "^\\(0\\d{2}\\)[- ]?\\d{8}$|^0\\d{2}[- ]?\\d{8}$|^\\(0\\d{3}\\)[- ]?\\d{7}$|^0\\d{3}[- ]?\\d{7}$";
            //Regex regex = new Regex(pattern);
            //return regex.IsMatch(input);
            Regex reg = new Regex(@"^(([\d]{3,4})?([-\s]?)[\d]{7,8})$");
            return reg.IsMatch(input);
        }
        #endregion

        #region 判断是否为有效的Email地址
        /// <summary>
        /// 判断是否为有效的Email地址
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool isEmail(string str)
        {
            string re = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
            return Regex.IsMatch(str, re);
        }
        #endregion

        #region 判断是否为有效的OICQ号
        /// <summary>
        ///判断是否为有效的OICQ号
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool isOICQ(string str)
        {
            string re = @"^\d{5,11}$";
            return Regex.IsMatch(str, re);
        }
        #endregion

        #region 判断是否为有效的手机号码
        /// <summary>
        /// 判断是否为有效的手机号码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool isMobile(string str)
        {
            string re = @"^1\d{10}$";
            return Regex.IsMatch(str, re);
        }
        #endregion

        #region 判断是否是中国邮政编码
        /// <summary>
        /// 判断是否是中国邮政编码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsPostCode(string input)
        {
            Regex reg = new Regex(@"^([\d]{6})$");
            return reg.IsMatch(input);
        }
        #endregion

        #region 判断是否为有效的URL
        /// <summary>
        ///判断是否为有效的URL
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool isURL(string str)
        {
            string re =
                @"^((http|https|ftp|mms):(//|\\)((\w)+[.]){1,}(net|com|cn|org|cc|tv|[0-9]{1,3})(((/[\~]*|\[\~]*)(\w)+)|[.](\w)+)*(((([?](\w)+){1}[=]*))*((\w)+){1}([\&](\w)+[\=](\w)+)*)*)$";
            return Regex.IsMatch(str, re);
        }
        #endregion

        #region 判断是否为标准时间格式
        /// <summary>
        /// 判断是否为标准时间格式，如： 2005-12-31 00:00:00
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static bool IsStanardDateTime(string ds)	 // 2005-12-31 00:00:00
        {
            string s = ds.Trim();
            if (s.Length != 19 || s[4] != '-' || s[7] != '-' || s[10] != ' ' || s[13] != ':' || s[16] != ':') return false;	// 长度或格式不符
            if (s[0] < '0' || s[0] > '9' || s[1] < '0' || s[1] > '9' || s[2] < '0' || s[2] > '9' || s[3] < '0' || s[3] > '9' || s[5] < '0' || s[5] > '9') return false;
            if (s[6] < '0' || s[6] > '9' || s[8] < '0' || s[8] > '9' || s[9] < '0' || s[9] > '9' || s[11] < '0' || s[11] > '9' || s[12] < '0' || s[12] > '9') return false;
            if (s[14] < '0' || s[14] > '9' || s[15] < '0' || s[15] > '9' || s[17] < '0' || s[17] > '9' || s[18] < '0' || s[18] > '9') return false;

            int year = Convert.ToInt32(s.Substring(0, 4));
            int month = Convert.ToInt32(s.Substring(5, 2));
            int day = Convert.ToInt32(s.Substring(8, 2));
            int hour = Convert.ToInt32(s.Substring(11, 2));
            int minus = Convert.ToInt32(s.Substring(14, 2));
            int second = Convert.ToInt32(s.Substring(17, 2));

            if (year < 0 || month < 1 || month > 12 || day < 1 || day > 31 || hour < 0 || hour > 24 || minus < 0 || minus > 60 || second < 0 || second > 60) return false;

            return true;
        }
        #endregion

        #region 验证身份证格式
        /// <summary>
        /// 验证身份证格式
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static bool IsIDCard(string Id)
        {
            if (Id.Length == 18)
            {
                bool check = CheckIDCard18(Id);
                return check;
            }
            else if (Id.Length == 15)
            {
                bool check = CheckIDCard15(Id);
                return check;
            }
            else
            {
                return false;
            }
        }
        private static bool CheckIDCard18(string Id)
        {
            long n = 0;
            if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false)
            {
                return false;//数字验证
            }
            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (address.IndexOf(Id.Remove(2)) == -1)
            {
                return false;//省份验证
            }
            string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == false)
            {
                return false;//生日验证
            }
            string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
            string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
            char[] Ai = Id.Remove(17).ToCharArray();
            int sum = 0;
            for (int i = 0; i < 17; i++)
            {
                sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
            }
            int y = -1;
            Math.DivRem(sum, 11, out y);
            if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
            {
                return false;//校验码验证
            }
            return true;//符合GB11643-1999标准
        }
        private static bool CheckIDCard15(string Id)
        {
            long n = 0;
            if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14))
            {
                return false;//数字验证
            }
            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";

            if (address.IndexOf(Id.Remove(2)) == -1)
            {
                return false;//省份验证
            }
            string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-");
            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == false)
            {
                return false;//生日验证
            }
            return true;//符合15位身份证标准
        } 
        #endregion
    }
}
